<?php


namespace apps\marriage\app\op\controller;


use apps\marriage\core\model\PlatformAccountBook;
use apps\marriage\core\service\PlatformAccountBookService;
use apps\marriage\core\service\UserService;

class StatisticsController extends BaseMarriageOpController
{
    /**
     * @var UserService
     */
    private $userService;

    /**
     * @var PlatformAccountBookService
     */
    private $platformAccountBookService;

    protected function init()
    {
        parent::init();
        $this->userService = service('User', SERVICE_NAMESPACE);
        $this->platformAccountBookService = service('PlatformAccountBook', SERVICE_NAMESPACE);
    }

    public function member()
    {
        $todayBeginTime = strtotime(date('Ymd 00:00:00'));
        $todayEndTime = strtotime(date('Ymd 23:59:59'));
        $yesterdayBeginTime = $todayBeginTime - 24*3600;
        $yesterdayEndTime = $todayEndTime - 24*3600;
        $register = [
            'all' => $this->userService->count(),
            'today' => $this->userService->count(['register_begin_time' => $todayBeginTime, 'register_end_time' => $todayEndTime]),
            'yesterday' => $this->userService->count(['register_begin_time' => $yesterdayBeginTime, 'register_end_time' => $yesterdayEndTime]),
        ];
        $login = [
            'today' => $this->userService->count(['login_begin_time' => $todayBeginTime, 'login_end_time' => $todayEndTime]),
            'yesterday' => $this->userService->count(['login_begin_time' => $yesterdayBeginTime, 'login_end_time' => $yesterdayEndTime]),
        ];
        return $this->success([
            'register' => $register,
            'login' => $login
        ]);
    }

    public function all()
    {
        $todayBeginTime = strtotime(date('Ymd 00:00:00'));
        $todayEndTime = strtotime(date('Ymd 23:59:59'));
        $yesterdayBeginTime = $todayBeginTime - 24*3600;
        $yesterdayEndTime = $todayEndTime - 24*3600;
        $register = [
            'all' => $this->userService->count(),
            'today' => $this->userService->count(['register_begin_time' => $todayBeginTime, 'register_end_time' => $todayEndTime]),
            'yesterday' => $this->userService->count(['register_begin_time' => $yesterdayBeginTime, 'register_end_time' => $yesterdayEndTime]),
        ];
        $login = [
            'today' => $this->userService->count(['login_begin_time' => $todayBeginTime, 'login_end_time' => $todayEndTime]),
            'yesterday' => $this->userService->count(['login_begin_time' => $yesterdayBeginTime, 'login_end_time' => $yesterdayEndTime]),
        ];

        $platformAccount = [
            'all' => $this->platformAccountBookService->sum('money', ['type' => PlatformAccountBook::TYPE_INCOME]),
            'yesterday' => $this->platformAccountBookService->sum('money', [
                'type' => PlatformAccountBook::TYPE_INCOME,
                'min_time' => $yesterdayBeginTime,
                'max_time' => $yesterdayEndTime
            ]),
        ];
        return $this->success([
            'user' => [
                'register' => $register,
                'login' => $login
            ],
            'platform_account' => $platformAccount
        ]);
    }
}